<?php
class ControllerNewsType extends Controller {
	private $error = array();

	public function index() {
		$this->language->load('news/type');
		
		$this->data['breadcrumbs'] = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
			'separator' => false
		);

		$type_id = isset($this->request->get['type_id']) ? $this->request->get['type_id'] : 0;
		
		$this->load->model('news/type');
		$news_type = $this->model_news_type->getNewsType($type_id);
		
		if ($news_type) {
			$this->load->model('news/category');
			$categories = $this->model_news_category->getCategories(array('filter_type_id' => $type_id));
				
			$this->data['breadcrumbs'][] = array(
				'text'      => $news_type['name'],
				'href'      => '#',
				'separator' => $this->language->get('text_separator')
			);
		
			$this->load->model('tool/image');
			
			$this->load->model('news/article');			
				
			$this->data['categories'] = array();
			foreach ($categories as $category){
				$article_list = $this->model_news_article->getArticles(array(
					'filter_type_id' 		=> $type_id,
					'filter_category_id'	=> $category['category_id'],
					'start' 		 		=> 0,
					'limit' 		 		=> 4
				));

				$articles = array();
				foreach ($article_list as $article_info){
					if (!empty($article_info['image']) && file_exists(DIR_IMAGE .$article_info['image'])) {
						$thumb = $this->model_tool_image->resize($article_info['image'], 150, 150);
					} else {
						$thumb = $this->model_tool_image->resize('no_image.jpg', 150, 150);
					}
					
					$articles[] = array(
						'article_id'	=> $article_info['article_id'],
						'category_id' 	=> $article_info['category_id'],
						'image'			=> $thumb,
						'date_added'	=> $article_info['date_added'],
						'title'			=> $article_info['title'],
						'header'		=> $article_info['header'],
						'href' 			=> $this->url->link('news/article', 'article_id=' .$article_info['article_id'])
					);					
				}
				
				$this->data['categories'][] = array(
					'category_id' 	=> $category['category_id'],
					'category_name'	=> $category['name'],
					'article_list'	=> $articles,
					'href' 			=> $this->url->link('news/category', 'category_id=' .$category['category_id'])
				);
				
			}			
			
			
			$this->document->addLink($this->url->link('news/type', 'type_id=' . $type_id), 'canonical');

			// Mapping all language variables
			$this->data = array_merge($this->data, $this->language->getData());
								
			if (file_exists(DIR_RESTAURANT_TEMPLATE . $this->config->get('config_template') . '/template/news/type.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/type.tpl';
			} else {
				$this->template =  'default/template/news/type.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);
					
			$this->response->setOutput($this->render());
		} else {
			$url = '';

			$this->data['breadcrumbs'][] = array(
        		'text'      => $this->language->get('text_error'),
				'href'      => '#',
        		'separator' => $this->language->get('text_separator')
			);

			$this->document->setTitle($this->language->get('text_error'));

			$this->data['heading_title'] = $this->language->get('text_error');

			$this->data['text_error'] = $this->language->get('text_error');

			$this->data['button_continue'] = $this->language->get('button_continue');

			$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_RESTAURANT_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
				
			$this->response->setOutput($this->render());
		}
	}
}
?>